package com.easylinkin.sm.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.sm.constant.FeedbackConstant;
import com.easylinkin.sm.entity.Customer;
import com.easylinkin.sm.entity.Feedback;
import com.easylinkin.sm.mapper.FeedbackMapper;
import com.easylinkin.sm.repository.FeedbackRepository;
import com.easylinkin.sm.service.FeedbackService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.morn.boot.data.DisplayableServiceSupport;
import site.morn.boot.rest.RestPage;
import site.morn.framework.context.AccountContext;
import site.morn.rest.RestModel;
import site.morn.rest.RestPageable;

/**
 * 意见反馈服务
 *
 * @author yezhihao
 * @since develop, 2019/8/19
 */
@Slf4j
@Service("feedbackServiceImpl")
@Transactional
public class FeedbackServiceImpl extends
    DisplayableServiceSupport<Feedback, Long, FeedbackRepository> implements
    FeedbackService {

  @Autowired
  private FeedbackMapper feedbackMapper;

  @Override
  public <S extends Feedback> S add(RestModel<S> restModel) {
    S model = restModel.getModel();
    model.setModifier(AccountContext.currentUser().getUsername());
    model.setCustomerId(AccountContext.current(Customer.class).getId());
    return this.add(model);
  }

  @Override
  public Page<Feedback> getFeedback(RestPage<Feedback> restPage) {
    RestPageable pageable = restPage.getPageable();
    Feedback feedback = restPage.getModel();
    Page page = new Page((long)(pageable.getPage() + 1), pageable.getSize());
    String beginTime = "";
    String endTime = "";
    if(restPage.getAttach().get(FeedbackConstant.BEGINTIME) != null){
      beginTime = restPage.getAttach().get(FeedbackConstant.BEGINTIME).toString();
      endTime = restPage.getAttach().get(FeedbackConstant.ENDTIME).toString();
    }
    page.setRecords(feedbackMapper.getFeedback(feedback.getModifier(), feedback.getCustomerId(),
        beginTime,
        endTime, page));
    return page;
  }

  @Override
  public String getDetails(Long id) {
    return feedbackMapper.getDetails(id);
  }
}
